package org.cll.lagou.study.minicat.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2020/7/2 21:32
 */
public abstract class AbstractHttpServlet implements IServlet {

    private static final Logger logger = LoggerFactory.getLogger(AbstractHttpServlet.class);

    private static final String METHOD_GET = "GET";
    private static final String METHOD_POST = "POST";

    /***
     * 处理Get请求
     * @param iRequest  请求
     * @param iResponse 响应
     * @throws IOException 返回响应异常
     */
    protected abstract void doGet(IRequest iRequest, IResponse iResponse) throws IOException;

    /***
     * 处理post请求
     * @param iRequest  请求
     * @param iResponse 响应
     * @throws IOException 返回响应异常
     */
    protected abstract void doPost(IRequest iRequest, IResponse iResponse) throws IOException;

    /**
     * 接收请求，返回结果
     * @param iRequest  请求
     * @param iResponse 响应
     */
    @Override
    public void service(IRequest iRequest, IResponse iResponse) throws IOException {
        if (METHOD_GET.equalsIgnoreCase(iRequest.getMethodType())) {
            doGet(iRequest, iResponse);
        } else if (METHOD_POST.equalsIgnoreCase(iRequest.getMethodType())) {
            doPost(iRequest, iResponse);
        } else {
            logger.error("****** 不支持{}类型的请求", iRequest.getMethodType());
        }
    }
}